package com.bai.ruiji.Service.impl;

import com.bai.ruiji.Common.CustomException;
import com.bai.ruiji.Service.CategoryServer;
import com.bai.ruiji.Service.DishService;
import com.bai.ruiji.Service.SetmealService;
import com.bai.ruiji.mapper.CategoryMapper;
import com.bai.ruiji.pojo.Category;
import com.bai.ruiji.pojo.Dish;
import com.bai.ruiji.pojo.Setmeal;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 27471
 * Time : 2022/10/10 13:24
 */
@Service
public class CategoryServerImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryServer {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前进行判断
     * @param ids
     */

    public void rmove(Long ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int count = dishService.count(dishLambdaQueryWrapper);
        //查看当前分类是否关联了菜品，若果已经关联抛出异常
        if (count>0){
            //已经关联了菜品，抛出异常
           throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        //查看当前分类是否关联了套餐，若果已经关联抛出异常
        if (count1>0){
            //已经关联了套餐，抛出异常
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }
        //正常删除
        //super调用父类中的removeByid()方法并且将ids传给父类
       super.removeById(ids);
    }
}
